import axios from "axios";
import { nanoid } from "nanoid";
export default{
    namespaced:true,
    actions:{
       addPersonByWName(context,value){
         if(value.name.indexOf('王')===0){
            context.commit('AddPerson',value)
         }else{
            alert('只能添加姓王的人员')
         }
       },
       addPServer(context){
            axios.get('https://api.uomg.com/api/rand.music?sort=热歌榜&format=json').then(
                response => {
                    response.data.data.id=nanoid();
                    context.commit('AddPerson',response.data.data)
                    console.log(response.data)
                },
                error => {
                    alert(error.message)
                }
            )
       },
    },
    //准备mutations对象——修改state中的数据,方法名称大写用来区分 action中的方法
    mutations : { 
        AddPerson(state,value){
            state.personList.unshift(value)
        },
    },



    
    state :{
        personList:[{id:'001',artistsname:'李荣浩',name:'年少有为',picurl:'http://p2.music.126.net/tt8xwK-ASC2iqXNUXYKoDQ==/109951163606377163.jpg',url:'http://music.163.com/song/media/outer/url?id=1293886117'}]
    },
     getters:{
       firstPersonName(state){
         return state.personList[0].name;
       }
    }
}